﻿package com.cornerstone.tcmsmap {
	import com.cornerstone.tcmsmap.DeleteWorkshopEvent;
	import fl.controls.Label;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.geom.ColorTransform
	import com.cornerstone.tcmsmap.ModeChangedEvent;
	import com.cornerstone.tcmsmap.DeleteButton;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	import flash.text.TextFormat;
	import flash.text.TextLineMetrics;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Bitmap; 
	import flash.display.BitmapData; 

	/**
	 * ...
	 * @author Jiang Hongfei <jianghongfei@gmail.com>
	 */
	internal final class Workshop extends Sprite {
		//{ constants
		private static const BTN_SIZE:int = 12;
		//}
				
		//{ state
		private var _id:String;
		private var _shopName:String;
		private var _status:int = 1;
		private var _tip:String;
		private var _icon:String;
		
		private var dir:String = "down";
		private var dist:int = 30;
		//}
		
		//{ controls
		private var _timer:Timer = new Timer(500, 1);
		private var _main:Main;
		private var _bgContainer:Sprite;
		private var _bgMask:Sprite;
		
		private var _format:TextFormat;
		private var _label:TextField;
		
		private var _bg:Bitmap;
		private var _thumbnail:Bitmap;
		
		private var _btnDelete:DeleteButton;
		private var _btnEdit:EditButton;
		
		private var _tooltip:Tooltip;
		//}
		
		public function Workshop(main:Main) {
			
			_main = main;
			main.addEventListener(ModeChangedEvent.MODE_CHANGED, modeChangedHandler);

			_bgMask = new Sprite();
			_bgMask.graphics.beginFill(0x000000, 1);
			_bgMask.graphics.drawRect(0, 0, 48, 68);
			_bgMask.graphics.endFill();
			this.addChild(_bgMask);
			
			_bgContainer = new Sprite();
			_bgContainer.mask = _bgMask
			this.addChild(_bgContainer);
			
			_format = new TextFormat();
			_format.color = Main.idealForegroundColor;
			_format.size = 20;
			
			_label = new TextField;	
			_label.backgroundColor = 0xcccccc;
			this.addChild(_label);
			_label.defaultTextFormat = _format;
			_label.selectable = false;
		}
		
		//{ getter & setter
		public function get id():String {
			return _id;
		}
		
		public function set id(value:String):void {
			_id = value;
			this.name = value;
		}
		
		public function get shopName():String {
			return _shopName;
		}
		
		public function set shopName(value:String):void {
			_shopName = value;
			_label.text = value;
			
			if (_label.textWidth > 40) { // 48 - _btn.width / 2
				_label.x = 0 - (_label.textWidth - 40);
			}
		}
		
		public function get xposition():Number {
			return (x / 1);
		}
		
		public function set xposition(value:Number):void {
			x = (value * 1);
		}
		
		public function get yposition():Number {
			return (y / 1);
		}
		
		public function set yposition(value:Number):void {
			y = (value * 1);
		}
		
		public function get status():int {
			return _status;
		}
		
		public function set status(value:int):void {
			_status = value;
			changeColor(value);
		}
		
		public function get tip():String 
		{
			return _tip;
		}
		
		public function set tip(value:String):void 
		{
			_tip = value;
		}
		
		public function get icon():String 
		{
			return _icon;
		}
		
		public function set icon(value:String):void 
		{
			_icon = value;
		}
		
		//} getter & setter
		
		//{ event handler
		public function loadComplete(e:Event):void {
			var li:LoaderInfo = e.currentTarget as LoaderInfo;
			
			setupBG(li.content);
		}
		
		private function mouseOverHandler(event:MouseEvent):void {
			if (_main.isInEditMode) {
				_btnDelete.visible = true;
				_btnEdit.visible = true;
			}
			
			_bgContainer.graphics.beginFill(Main.hoverBackgroundColor, 0.75);
			_bgContainer.graphics.drawRect(0, 0, _bgContainer.width, _bgContainer.height + _bg.y);
			_bgContainer.graphics.endFill();
			//changeColor(OVER);
			
			if (_tip !== null && _tip.length > 0) {
				// startTooltipCounter
				_timer.addEventListener(TimerEvent.TIMER_COMPLETE, showTooltip);
				_timer.start();
			}
		}
		
		private function mouseOutHandler(event:MouseEvent):void {
			if (_main.isInEditMode) {
				_btnDelete.visible = false;
				_btnEdit.visible = false;
			}
			
			_bgContainer.graphics.clear();
			
			changeColor(_status);
			
			// hideTooltip
			if (_timer.currentCount == 1)
			{
				removeChild(_tooltip);
			}

			_timer.reset();
		}
		
		private function showTooltip(e:TimerEvent):void
		{
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, showTooltip);
			
			/* Modify the parameters to get new tooltips */
			
			_tooltip = new Tooltip(_tip); //To set a new line use \n, i.e "Flash\nTuts"
			
			/* Start Position */
			
			switch (dir)
			{
				case "up" :
					_tooltip.x = mouseX - _tooltip.width / 2;
					_tooltip.y = mouseY - (_tooltip.height + dist);
					break;
				case "down" :
					_tooltip.x = mouseX - _tooltip.width / 2;
					_tooltip.y = _bgMask.mouseY + dist;
					break;
				case "left" :
					_tooltip.x = mouseX - _tooltip.width - dist;
					_tooltip.y = mouseY - (_tooltip.height / 2);
					break;
				case "right" :
					_tooltip.x = mouseX + dist;
					_tooltip.y = mouseY - (_tooltip.height / 2);
					break;
			}
			
			/* Add tooltip to stage and move listener */
			
			addChild(_tooltip);
			
			this.addEventListener(MouseEvent.MOUSE_MOVE, moveTooltip);
		}
		
		
		private function moveTooltip(e:MouseEvent):void
		{
			switch (dir)
			{
				case "up" :
					_tooltip.x = mouseX - _tooltip.width / 2;
					_tooltip.y = mouseY - (_tooltip.height + dist);
					break;
				case "down" :
					_tooltip.x = mouseX - _tooltip.width / 2;
					_tooltip.y = _bgMask.mouseY + dist;
					break;
				case "left" :
					_tooltip.x = mouseX - _tooltip.width - dist;
					_tooltip.y = mouseY - (_tooltip.height / 2);
					break;
				case "right" :
					_tooltip.x = mouseX + dist;
					_tooltip.y = mouseY - (_tooltip.height / 2);
					break;
			}
		}
		
		public function modeChangedHandler(event:ModeChangedEvent):void {
			trace(event.isInEditMode ? "edit" : "normal");
		}
				
		private function mouseDownHandler(event:MouseEvent):void {	
			if (_main.isInEditMode) {
				this.startDrag();
			} else {
				var url:URLRequest = new URLRequest(_main.targetURL + "/" + shopName);
				navigateToURL(url, "_blank");
			}
		}
		
		private function mouseUpHandler(event:MouseEvent):void {
			if (_main.isInEditMode) {
				this.stopDrag();
			}
		}
		
		private function onDelete(event:MouseEvent):void {
			dispatchEvent(new DeleteWorkshopEvent(DeleteWorkshopEvent.DELETE, this));
		}
		
		private function onEdit(event:MouseEvent):void {
			dispatchEvent(new EditWorkshopEvent(EditWorkshopEvent.EDIT, this));
		}
		//}
		
		//{ methods
		private function changeColor(value:int):void {
			_label.textColor = value === 0 ? Main.idealForegroundColor : Main.busyForegroundColor;
			if (_bg !== null) {
				_bg.x = 0 - value * 48;
			}
			// 1: ideal
			// 2: busy
			//_thumbnail.gotoAndStop(value + 1);
		}
		
		public function setupBG(content:DisplayObject):void {
			_thumbnail = content as Bitmap;
			
			_bg = new Bitmap(_thumbnail.bitmapData);
			_bg.x = 0 - this.status * 48;
			_bg.y = 20;
			_bgContainer.addChild(_bg);
			
			if (_main.isInEditMode) {
				_btnDelete = new DeleteButton();
				this.addChild(_btnDelete);
				_btnDelete.x = 48 - _btnDelete.width / 2;			
				_btnDelete.y = 0 - _btnDelete.height / 2;
				_btnDelete.visible = false;
				_btnDelete.addEventListener(MouseEvent.CLICK, onDelete);
				
				_btnEdit = new EditButton();
				this.addChild(_btnEdit);
				_btnEdit.x = 32 - _btnEdit.width / 2;
				_btnEdit.y = 0 - _btnEdit.height / 2;
				_btnEdit.visible = false;
				_btnEdit.addEventListener(MouseEvent.CLICK, onEdit);
			}
			
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}

		public function updateBG(content:DisplayObject):void {
			_thumbnail = content as Bitmap;
			
			// TODO: _bgContainer.removeChild(_bg) dosen't work, it's weird
			while (_bgContainer.numChildren > 0) {
				_bgContainer.removeChildAt(0);
			}

			_bg = new Bitmap(_thumbnail.bitmapData);
			_bg.x = 0 - this.status * 48;
			_bg.y = 20;
			_bgContainer.addChild(_bg);
		}
		//}
	}

}